package descarte.com.br.descarte;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * Created by kcpo on 22/03/2015.
 */
public class MapaReciclagemActivity extends FragmentActivity {

    SupportMapFragment map;
    GoogleMap mapa;
    Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_maps);

        ActionBar actionBar = getActionBar();
        actionBar.setTitle("NOVA RECICLAGEM");
        actionBar.setDisplayHomeAsUpEnabled(true);

        GoogleMapOptions options = new GoogleMapOptions();
        options.zOrderOnTop(true);

        map = SupportMapFragment.newInstance(options);
        android.support.v4.app.FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        trans.replace(R.id.fragMapa,map);
        trans.commit();


    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(){
            public void run(){
                while(map.getMap() == null){
                    try{
                        Thread.sleep(1000);
                    }catch(Exception e){

                    }
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        configMap();
                    }
                });
            }
        }.start();
    }

    public void configMap(){
        mapa = map.getMap();
        mapa.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mapa.setMyLocationEnabled(true);


        LatLng latNg = new LatLng(-8.1192006, -34.9049338);

        CameraPosition posistion = new CameraPosition.Builder().target(latNg).zoom(15).build();
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(posistion);


        //mapa.moveCamera(update);
        mapa.animateCamera(update, 3000, new GoogleMap.CancelableCallback() {
            @Override
            public void onFinish() {

            }

            @Override
            public void onCancel() {

            }
        });


        mapa.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if(marker != null){
                    marker.remove();
                }

                addMarck(new LatLng(latLng.latitude,latLng.longitude),"Seu Endereço","Click para salvar");
            }
        });

        mapa.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {

            }
        });

    }

    public void addMarck(LatLng lt, String titule, String snipet){
        MarkerOptions options = new MarkerOptions();
        options.position(lt).title(titule).snippet(snipet).draggable(false);

        if(PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.METAL)){
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_metal));
        } else  if(PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.PAPEL)){
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_papel));
        }else  if(PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.PLASTICO)){
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_plastico));
        }else  if(PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.VIDRO)){
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_vidro));
        }

        marker = mapa.addMarker(options);


    }



}
